<?php


namespace Leave\Controller;

use Think\Controller;


class IndexController extends Controller
{
    /**
     * 业务逻辑都放在 WeiboApi 中
     * @var
     */
    // const MODEL_NAME = 'Leave@:';
    public function index()
    {
        //  if(I('keyword')){    //查询关键字进行搜索。
        //     $keyword = I('keyword');
        //     $map['reason'] = array('like','%'.$keyword.'%');
        //     $this->assign('keyword',$keyword);
        // }
        // if(I('createtime')&&I('endDate')){
        //     $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
        // }
        // if(I('sort')){
        //     $sort = I('sort');
        //     if($sort == 'recycle'){
        //         $map['status'] = -1;

        //         $this->assign('recycle','【回收站】');
        //     }
        // }else{
        //     $sort = 'normal';
        //     $map['status'] = array('egt', 0);
        // }            
        $leave = M('core_leave_view');

        $map['unit_id'] = $_SESSION['oc_home']['unit'];
        $map['status']=array('in','1,0');
        if($_SESSION['oc_home']['group']=='4'){
            $map['class_id'] = $_SESSION['oc_home']['class'];
        }
        if(I('status')!=''){
            $map2['status'] = I('status');
        }else{
            $map2['status']=array('in','1,0');
        }
        $map3['status']=1;
        $map4['status']=0;
        // $count      = $leave->where($map)->where($map2)->count();// 查询满足要求的总记录数
        $succ      = $leave->where($map)->where($map3)->count();// 查询满足要求的总记录数
        $erro      = $leave->where($map)->where($map4)->count();// 查询满足要求的总记录数

        // $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        // $show       = $Page->show();// 分页显示输出
        $data      = $leave->where($map)->order('status desc')->select();
        $count=count($data);
        $list       = $leave->where($map)->where($map2)->order('status desc')->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('succ',$succ);
        $this->assign('erro',$erro);
        $this->display();
    }

    public function addrecord()
    {
        header("Content-type: text/html; charset=utf-8");
        if(IS_POST){
            $dd['uid']=$_SESSION["oc_home"]['uid'];
            $dd['leave_date']=strtotime(I('leave_date'));
            $dd['create_time']=time();
            $dd['days']=I('days');
            $dd['reason']=I('reason');
            $dd['type']=I('type');
            $d=M('core_leave')->add($dd);
            // $this->success('添加成功',U('editimpl'));
            echo "<script>";
            echo"alert('请假成功');";
            echo "window.location.href='./index.php?s=/Leave/Index/editimpl&id=".$d."'; ";
            echo"</script>";

        }else{
            $typeList = M('core_leave_type')->where('status='.'1')->select();
            $this->assign('typeList',$typeList);
            $this->display();   
        }

    }
     public function editimpl()
    {
        $list=M('core_leave')->where('id='.I('id'))->find();
        $leave = M('core_leave_type')->where('id = '.$list['type'])->getField('title');
        $this->assign('list',$list);
        $this->assign('leave',$leave);
        $this->display();
       
    }
    public function leavelist(){
        header("Content-type: text/html; charset=utf-8");
        
        $leave = M('core_leave_view');
        $map['status']=array('eq',0);
        $map['unit_id']=$_SESSION['oc_home']['unit'];
        $map['class_id']=$_SESSION['oc_home']['class'];
        $count       = $leave->where($map)->count();
        $list       = $leave->where($map)->select();
        foreach ($list as $key => $value) {
            $leave = M('core_leave_type')->where('id = '.$value['type'])->getField('title');
            $list[$key]['lname'] = $leave;
        }
         $this->assign('list',$list);
         $this->assign('count',$count);
         $this->display();
    }
    public function leave()
    {
        header("Content-type: text/html; charset=utf-8");
        if(IS_POST){
            $data['status'] = I('status');
            $result = M('core_leave')->where('id = '.I('tid'))->save($data);
            if($result){
                echo "<script>";
                echo"alert('更改状态成功');";
                echo "window.location.href='./index.php?s=/Leave/Index/leaveList'; ";
                echo"</script>";
            }else{
                echo "<script>";
                echo"alert('更改状态失败');";
                echo "window.location.href='./index.php?s=/Leave/Index/leaveList'; ";
                echo"</script>";
            }
        }else{
            $leave = M('core_leave')->where('id = '.I('id'))->find();
            $leave['lname'] = M('core_userinfo_view')->where('uid ='.$leave['uid'])->getField('nickname');
            $this->assign('leave',$leave);
            $this->assign('id',I('id'));
            $this->display();
        }
       
    }
    public function collect()
    {
        $map['uid']=$_SESSION["oc_home"]['uid'];
        $map['status']=array('in','1,0');
        if(I('status')!=''){
            $map2['status'] = I('status');
        }else{
            $map2['status']=array('in','1,0');
        }    
        $count=M('core_leave')->where($map)->count();
        $succ=M('core_leave')->where($map)->where('status=1')->count();
        $erro=M('core_leave')->where($map)->where('status=0')->count();
        $list=M('core_leave')->where($map)->where($map2)->select();
        foreach ($list as $key => $value) {
            $leave = M('core_leave_type')->where('id = '.$value['type'])->getField('title');
            $leav=M('core_userinfo_view')->where('uid='.$value['uid'])->getField(nickname);
            $list[$key]['lname'] = $leave;
            $list[$key]['name']=$leav;
        }
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('succ',$succ);
        $this->assign('erro',$erro);
        $this->display();
    }
    public function details()
    {
       $this->display();
    }
    public function DeanAskForLeave()
    {
        header("Content-type: text/html; charset=utf-8");

        if(I('nickname')){  
            $map['nickname'] = array('like','%'.I('nickname').'%');
            $this->assign('nickname',I('nickname'));
        }
        if(I('createtime')&&I('endDate')){
            $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
            $this->assign('createtime',I('createtime'));
            $this->assign('endDate',I('endDate'));
        }

        switch ($_SESSION['oc_home']['group']) {
            case '6':
                $fy = M('core_unit')->where('pid='.$_SESSION['oc_home']['unit'])->getField('id',true);
                //查总园下面所有分园
                if(I('unit')){  
                    $map['unit_id'] = I('unit');
                    $this->assign('unit',I('unit'));
                }else{
                    $map['unit_id'] = array('in',implode(',',$fy));
                }
                if(I('class_list')){  
                    $map['class_id'] = I('class_list');
                    $this->assign('class_list',I('class_list'));
                }
                $list = M('core_leave_view')->where($map)->select();
                //查班级用
                $_SESSION['oc_home']['fy'] = implode(',',$fy);
                break;
            case '3':
                $map['unit_id'] = $_SESSION['oc_home']['unit'];
                if(I('class')){  
                    $map['class_id'] = I('class');
                    $this->assign('class',I('class'));
                }
                $list = M('core_leave_view')->where($map)->select();
                //查班级用
                $_SESSION['oc_home']['fy'] = $_SESSION['oc_home']['unit'];
                break;                
        }
        
        //查班级
        $con['unit'] = array('in',$_SESSION['oc_home']['fy']);
        $classList = M('core_class')->where($con)->field('id,title')->select();
        // $classList = M('core_class')->where($con)->getField('title',true);
        //查分园
        $con2['id'] = array('in',$_SESSION['oc_home']['fy']);
        $unitList = M('core_unit')->where($con2)->field('id,title')->select();
        // $unitList = M('core_unit')->where($con2)->getField('title',true);
        $count=count($list);
        $this->assign('list',$list);
        $this->assign('classList',$classList);
        $this->assign('unitList',$unitList);
        $this->assign('count',$count);
        $this->display();     
    }
}

